package setup

import (
	"dha/app/models"
	"dha/pub"
	"dha/pub/log"
	"dha/utils"
	"fmt"
)

func InitUser() {
	salt := "deepin.home.admin"
	username := "admin"
	password := "deepin@home.admin"

	// 执行初始化时,先删除有可能存在的用户
	err := pub.DB().Unscoped().Where("username = ?", username).Delete(&models.User{}).Error
	if err != nil {
		panic(err)
	}

	passHash := utils.HashStr(password, salt)
	var user = &models.User{
		Username: username,
		Password: passHash,
		Salt:     salt,
	}
	err = pub.DB().Create(user).Error
	if err != nil {
		panic(err)
	}
	log.Info("初始化数据库和默认用户完成")
	fmt.Println("\n\n***********************************")
	fmt.Printf("默认用户: %s\n默认密码: %s\n", username, password)
	fmt.Printf("***********************************\n\n")
}
